﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using System.Runtime.InteropServices;

namespace ConsoleApplication
{
    public class StringClass
    {
        public void CharShow()
        {
            char c = '1';
            Console.WriteLine(Char.GetUnicodeCategory(c));
            Console.WriteLine(Char.Parse("t"));
        }
        public void StringShow()
        {
            string s = "Hi here!" + System.Environment.NewLine + "haha";
            Console.WriteLine(s);
            string s1 = "Hello";
            string s2 = "Hello";
            s1 = String.Intern(s1);
            Console.WriteLine(Object.ReferenceEquals(s1, s2));
            System.Globalization.StringInfo si = new System.Globalization.StringInfo(s1);
            for (int i = 0; i < si.LengthInTextElements; i++)
            {
                s2 += si.SubstringByTextElements(i, 1);
            }
            Console.WriteLine(s2);
        }
        public void StringBuilderShow()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("a");
            sb.Append("b");
            Console.WriteLine(sb.ToString());
            sb.Append("c");
            Console.WriteLine(sb.ToString());
        }
        public void OthersShow()
        {
            int i = 100;
            Console.WriteLine(i.ToString());
            Object o = new Object();
            o = i;
            Console.WriteLine(o.ToString());
            DateTime dt = DateTime.Parse("2009-04-01");
            Console.WriteLine(dt.ToString());
            byte[] data = Encoding.UTF8.GetBytes("Hi");
            Console.WriteLine(BitConverter.ToString(data));
            Console.WriteLine(Encoding.UTF8.GetByteCount("how are you?"));
            Console.WriteLine(Convert.ToBase64String(data));
        }
        public void SecureStringShow()
        {
            using (SecureString ss = new SecureString())
            {
                Console.Write("Please enter password:");
                while (true)
                {
                    ConsoleKeyInfo cki = Console.ReadKey(true);
                    if (cki.Key == ConsoleKey.Enter)
                    {
                        break;
                    }
                    ss.AppendChar(cki.KeyChar);
                    Console.Write("*");
                }
                Console.WriteLine();
                DisplaySecureString(ss);
            }
        }
        private unsafe void DisplaySecureString(SecureString ss)
        {
            Char* pc = null;
            try
            {
                pc = (Char*)Marshal.SecureStringToGlobalAllocUnicode(ss);
                for (int i = 0; pc[i] != 0; i++)
                {
                    Console.Write(pc[i]);
                }
            }
            finally
            {
                if (pc != null)
                {
                    Marshal.ZeroFreeCoTaskMemUnicode((IntPtr)pc);
                }
            }
        }
    }
}
